﻿Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Formlogin.txtpermis.Text.Trim = "User" Then
            tabholiday.Visible = False
            tabuser.Visible = False
            tabstaff.Visible = False
            formsetting.tabtime.Visible = False
            loadatd()
        Else
            loademployee()
            loadholiday()
            loadatd()
            loaduser()
        End If
        ch_time_ava()
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
    End Sub
    Public Sub loademployee()
        'emp data==============================================================
        CMD.Parameters("@s") = "%" & txtemp.Text & "%"
        dgvemp.DataSource = CMD.ExecuteDataTable("SELECT e.EMP_ID,e.EMP_CODE,e.EMP_NAME,e.IMAGE,e.EMP_SEX,p.POS_NAME,e.DOB,e.EMP_ADR,e.EMP_PHONE,e.SALARY FROM TBL_EMPLOYEE e INNER JOIN TBL_POSITION p ON(e.POS_ID=p.POS_ID) WHERE EMP_NAME like @s OR EMP_CODE LIKE @S")
        f_dgv.FormatDataGrid(dgvemp)
    End Sub
    Public Sub loadholiday()
        'emp data==============================================================

        dgvholiday.DataSource = CMD.ExecuteDataTable("SELECT * from TBL_HOLIDAY")
        f_dgv.FormatDataGrid(dgvholiday)
    End Sub
    Public Sub loadatd()
        'emp data==============================================================
        If Formlogin.txtpermis.Text.Trim = "User" Then
            CMD.Parameters("@code") = Formlogin.txtcode.Text.Trim
            dgvatt.DataSource = CMD.ExecuteDataTable("SELECT a.ATD_ID,e.EMP_CODE,e.EMP_NAME,a.ATD_DATE,a.M_IN,a.M_OUT,a.A_IN,a.A_OUT from TBL_ATD a Inner join TBL_EMPLOYEE e on(a.EMP_ID=e.EMP_ID) WHERE e.EMP_CODE=@code")
            f_dgv.FormatDataGrid(dgvatt)
            'For i As Integer = 0 To dgvatt.RowCount - 1
            '    MsgBox(dgvatt.Rows(i).Cells(3))
            'Next
        Else
            dgvatt.DataSource = CMD.ExecuteDataTable("SELECT a.ATD_ID,e.EMP_CODE,e.EMP_NAME,a.ATD_DATE,a.M_IN,a.M_OUT,a.A_IN,a.A_OUT from TBL_ATD a Inner join TBL_EMPLOYEE e on(a.EMP_ID=e.EMP_ID)")
            f_dgv.FormatDataGrid(dgvatt)
            For i As Integer = 0 To dgvatt.RowCount - 1
                ''Dim min As DateTime = dgvatt.Rows(i).Cells("M_OUT").Value
                ''Dim out As DateTime = dgvatt.Rows(i).Cells("M_IN").Value
                ''dgvatt.Rows(i).Cells("M_TOTAL").Value = out.ToLongTimeString.ToString

            Next
        End If
        

    End Sub
    Public Sub loaduser()
        'emp data==============================================================
        CMD.Parameters("@user") = "%" & txtuser.Text & "%"
        dgvuser.DataSource = CMD.ExecuteDataTable("SELECT u.U_ID,e.EMP_NAME,u.LOG_NAME,u.PWD,u.U_PERMIS FROM TBL_USER u inner join TBL_EMPLOYEE e on(u.EMP_ID=e.EMP_ID) WHERE u.LOG_NAME LIKE @user")
        f_dgv.FormatDataGrid(dgvuser)
    End Sub
    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        frmstaffeditor.clear()
        If frmstaffeditor.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        If Me.dgvemp.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgvemp.SelectedRows(0)
        Dim frm As New frmstaffeditor()
        frm.txtid.Text = row.Cells("EMP_ID").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        End If
    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click
        If Me.dgvemp.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Dim row = Me.dgvemp.SelectedRows(0)
        Dim id = row.Cells("EMP_ID").Value
        CMD.Parameters("@ID") = id
        CMD.ExecuteNonQuery("DELETE From TBL_EMPLOYEE WHERE EMP_ID=@ID")
        loademployee()
    End Sub
    Private Sub ButtonX6_Click(sender As Object, e As EventArgs) Handles ButtonX6.Click
        If HollidayEdotor.ShowDialog = Windows.Forms.DialogResult.OK Then

        End If
    End Sub

    Private Sub ButtonX5_Click(sender As Object, e As EventArgs) Handles ButtonX5.Click
        If Me.dgvholiday.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Dim row = Me.dgvholiday.SelectedRows(0)
        Dim id = row.Cells("H_ID").Value
        CMD.Parameters("@ID") = id
        CMD.ExecuteNonQuery("DELETE From TBL_HOLIDAY WHERE H_ID=@ID")
        Me.loadholiday()
    End Sub

    Private Sub ButtonX4_Click(sender As Object, e As EventArgs) Handles ButtonX4.Click
        If Me.dgvholiday.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgvholiday.SelectedRows(0)
        Dim frm As New HollidayEdotor()
        frm.txth_id.Text = row.Cells("H_ID").Value
        frm.DTh_date.Text = row.Cells("H_DATE").Value
        frm.txth_desc.Text = row.Cells("DESC").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        End If
    End Sub

    Private Sub TabControl1_SelectedTabChanged(sender As Object, e As DevComponents.DotNetBar.TabStripTabChangedEventArgs) Handles TabControl1.SelectedTabChanged
        If TabControl1.SelectedTabIndex = 5 Then
            formsetting.ShowDialog()
        End If
        process.Timer1.Interval = 1500
        process.ShowDialog()
       

    End Sub
    Private Sub ButtonX9_Click(sender As Object, e As EventArgs) Handles ButtonX9.Click
        formusereditor.ShowDialog()
    End Sub

    Private Sub ButtonX8_Click(sender As Object, e As EventArgs) Handles ButtonX8.Click
        If Me.dgvuser.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Dim row = Me.dgvuser.SelectedRows(0)
        Dim id = row.Cells("U_ID").Value
        CMD.Parameters("@ID") = id
        CMD.ExecuteNonQuery("DELETE From TBL_USER WHERE U_ID=@ID")
        loaduser()
    End Sub
    Private Sub ButtonX7_Click(sender As Object, e As EventArgs) Handles ButtonX7.Click
        If Me.dgvuser.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = dgvuser.SelectedRows(0)
        Dim frm As New formusereditor()
        frm.txtid.Text = row.Cells("U_ID").Value
        If (frm.ShowDialog() = Windows.Forms.DialogResult.OK) Then

        End If
    End Sub
    Public Sub ch_time_ava()
        CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
        CMD.Parameters("@tod") = Today
        Dim countd = CMD.ExecuteScalar("select count(*) from TBL_ATD WHERE EMP_ID=@emp_id AND ATD_DATE=@tod")
        If countd > 0 Then
            CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
            CMD.Parameters("@tod") = Today
            Dim dr = CMD.ExecuteDataReader("select * from TBL_ATD WHERE EMP_ID=@emp_id AND ATD_DATE=@tod")
            While dr.Read
                If TimeOfDay < times.m_in Then
                    btnc_in.Enabled = False
                    btnc_out.Enabled = False
                ElseIf TimeOfDay < times.m_out Then
                    If dr(3).ToString = "00:00:00" Then
                        btnc_in.Enabled = True
                        btnc_out.Enabled = False
                    ElseIf dr(4).ToString = "00:00:00" Then
                        btnc_in.Enabled = False
                        btnc_out.Enabled = True
                    Else
                        btnc_in.Enabled = False
                        btnc_out.Enabled = False
                    End If
                ElseIf TimeOfDay < times.a_in Then
                    btnc_in.Enabled = False
                    btnc_out.Enabled = False
                ElseIf TimeOfDay < times.a_out Then
                    If dr(5).ToString = "00:00:00" Then
                        btnc_in.Enabled = True
                        btnc_out.Enabled = False
                    ElseIf dr(6).ToString = "00:00:00" Then
                        btnc_in.Enabled = False
                        btnc_out.Enabled = True
                    Else
                        btnc_in.Enabled = False
                        btnc_out.Enabled = False
                    End If
                Else
                    btnc_in.Enabled = False
                    btnc_out.Enabled = False
                End If
            End While
            dr.Close()
        Else
            btnc_in.Enabled = True
            btnc_out.Enabled = False
        End If
    End Sub
    Private Sub ButtonX10_Click(sender As Object, e As EventArgs) Handles btnc_in.Click
        CMD.Parameters("@tod") = Today
        Dim ho = CMD.ExecuteScalar("select count(*) from TBL_HOLIDAY Where H_DATE=@tod")
        Dim day = Format(Today, "dddd")
        If ho > 0 Or day = "Sunday" Then
            MsgBox("Today is Holiday...!")
        Else
            CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
            CMD.Parameters("@tod") = Today
            Dim countd = CMD.ExecuteScalar("select count(*) from TBL_ATD WHERE EMP_ID=@emp_id AND ATD_DATE=@tod")
            If countd > 0 Then
                If TimeOfDay < times.a_in Then
                    MsgBox("please check in again at 1:00")
                ElseIf TimeOfDay < times.m_out Then
                    CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
                    CMD.Parameters("@tod") = Today
                    CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
                    CMD.ExecuteNonQuery("UPDATE TBL_ATD SET M_IN=@time WHERE EMP_ID=@emp_id AND ATD_DATE=@tod")
                    Me.loadatd()
                    btnc_in.Enabled = False
                    btnc_out.Enabled = True
                ElseIf TimeOfDay < times.a_out Then
                    CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
                    CMD.Parameters("@tod") = Today
                    CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
                    CMD.ExecuteNonQuery("UPDATE TBL_ATD SET A_IN=@time WHERE EMP_ID=@emp_id AND ATD_DATE=@tod")
                    Me.loadatd()
                    btnc_in.Enabled = False
                    btnc_out.Enabled = True
                End If
            Else

                If TimeOfDay < times.m_in Then
                    MsgBox(M_IN)
                    MsgBox("check in again at 7:00 ...!")
                ElseIf TimeOfDay < times.m_out Then
                    CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
                    CMD.Parameters("@tod") = Today
                    CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
                    CMD.ExecuteNonQuery("INSERT INTO TBL_ATD VALUES(@emp_id,@tod,@time,'00:00','00:00','00:00')")
                    Me.loadatd()
                    btnc_in.Enabled = False
                    btnc_out.Enabled = True
                ElseIf TimeOfDay < times.a_in Then
                    MsgBox("please check in again at 13:00")
                ElseIf TimeOfDay < times.a_out Then
                    CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
                    CMD.Parameters("@tod") = Today
                    CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
                    CMD.ExecuteNonQuery("INSERT INTO TBL_ATD VALUES(@emp_id,@tod,'00:00','00:00',@time,'00:00')")
                    Me.loadatd()
                    btnc_in.Enabled = False
                    btnc_out.Enabled = True
                End If
            End If
        End If
    End Sub
    Private Sub ButtonX11_Click(sender As Object, e As EventArgs) Handles btnc_out.Click
       
        If TimeOfDay <= times.m_out Then
            CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
            CMD.Parameters("@tod") = Today
            CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
            CMD.ExecuteNonQuery("UPDATE TBL_ATD SET M_OUT=@time WHERE EMP_ID=@emp_id AND ATD_DATE=@tod ")
            Me.loadatd()
            btnc_in.Enabled = False
            btnc_out.Enabled = False
        ElseIf TimeOfDay <= times.a_out Then
            CMD.Parameters("@emp_id") = Formlogin.txtemp_id.Text
            CMD.Parameters("@tod") = Today
            CMD.Parameters("@time") = FormatDateTime(TimeOfDay)
            CMD.ExecuteNonQuery("UPDATE TBL_ATD SET A_OUT=@time WHERE EMP_ID=@emp_id AND ATD_DATE=@tod ")
            Me.loadatd()
            btnc_in.Enabled = False
            btnc_out.Enabled = False
        End If

    End Sub
  
    Private Sub TabControlPanel1_Click(sender As Object, e As EventArgs) Handles TabControlPanel1.Click

    End Sub

    Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
    End Sub

    Private Sub txtemp_TextChanged(sender As Object, e As EventArgs)
        loademployee()
    End Sub
    Private Sub txtuser_TextChanged(sender As Object, e As EventArgs) Handles txtuser.TextChanged
        loaduser()
    End Sub

    Private Sub txtholi_TextChanged(sender As Object, e As EventArgs)
        loadholiday()
    End Sub

    Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub TabControlPanel3_Click(sender As Object, e As EventArgs) Handles TabControlPanel3.Click

    End Sub
End Class
